package com.hucheng.rest.global;

import com.hucheng.common.exception.EduException;
import com.hucheng.common.response.ServiceCoreVO;
import com.hucheng.common.result.ReturnVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-06-11
 * @Description: 全局异常捕获
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 数据库异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = DataAccessException.class)
    public ServiceCoreVO sqlExceptionHandler(DataAccessException e) {
        return new ServiceCoreVO(e.getMessage());
    }

    /**
     * 自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = EduException.class)
    public ServiceCoreVO customExceptionHandler(EduException e) {
        return new ServiceCoreVO(Integer.parseInt(e.getMessage()));
    }

    /**
     * 系统异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ServiceCoreVO systemExceptionHandler(Exception e) {
        return new ServiceCoreVO(String.valueOf(e));
    }

}
